package com.example.book_libapp;

import org.json.JSONException;
import org.json.JSONObject;


import com.example.database.library.DatabaseHandler;
import com.example.database.library.UserFunctions;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Register extends Activity implements OnClickListener{
	
	private EditText inputFullName;
	private EditText inputEmail;
	private EditText inputPassword;
	private EditText inputRepassword;
	private Button btnregister;
	
	// JSON Response node names
	private static String KEY_SUCCESS = "success";
	private static String KEY_ERROR = "error";
	private static String KEY_ERROR_MSG = "error_msg";
	private static String KEY_UID = "uid";
	private static String KEY_NAME = "name";
	private static String KEY_EMAIL = "email";
	private static String KEY_CREATED_AT = "created_at";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);
		
		inputEmail = (EditText)findViewById(R.id.EditMail);
		inputFullName = (EditText)findViewById(R.id.EditName);
		inputPassword = (EditText)findViewById(R.id.EditPass);
		btnregister = (Button)findViewById(R.id.BtnRegister);
		btnregister.setOnClickListener(this);
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == btnregister) {
			String name = inputFullName.getText().toString();
			String email = inputEmail.getText().toString();
			String password = inputPassword.getText().toString();
			UserFunctions userFunction = new UserFunctions();
			JSONObject json = userFunction.registerUser(name, email, password);
			
			// check for login response
			try {
				if (json.getString(KEY_SUCCESS) != null) {
		//			registerErrorMsg.setText("");
					String res = json.getString(KEY_SUCCESS); 
					if(Integer.parseInt(res) == 1){
//						 user successfully registred
//						 Store user details in SQLite Database
						DatabaseHandler db = new DatabaseHandler(getApplicationContext());
						JSONObject json_user = json.getJSONObject("user");
						
						// Clear all previous data in database
						userFunction.logoutUser(getApplicationContext());
						db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));						
						// Launch Dashboard Screen
						Intent i = new Intent(getApplicationContext(), ListViewBookActivity.class);
						// Close all views before launching Dashboard
//						dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
						startActivity(i);
						// Close Registration Screen
						finish();
					}else{
						// Error in registration
//						registerErrorMsg.setText("Error occured in registration");
					}
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

	}
}


